//
//  DSTextField.m
//  Competition
//
//  Created by 段帅 on 16/4/14.
//  Copyright © 2016年 newfuture. All rights reserved.
//

#import "DSTextField.h"
#define Color333333 [UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1.0f]
#define NormalFont(A) [UIFont systemFontOfSize:A/3]

#define ScaleSize(A) 0.383*A

@implementation DSTextField


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    
    return self;
    
}
-(void)createUI{
    self.selected = NO;
    self.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    CGRect frame = [self frame];
    
    frame.size.width = ScaleSize(30);
//    [self setValue:colordbdbdb forKeyPath:@"_placeholderLabel.textColor"];
//    [self setValue:NormalFont(40) forKeyPath:@"_placeholderLabel.font"];
//    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    UIView *leftview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScaleSize(30), ScaleSize(96))];
    self.leftViewMode = UITextFieldViewModeAlways;
    
    
    self.leftView = leftview;
    
    self.font = NormalFont(42);
    self.textColor = Color333333;
    
    [self addTarget:self 
     
             action:@selector(textFieldDidChange:)
     
         forControlEvents:UIControlEventEditingChanged];
    
    
    
}

-(void)textFieldDidChange:(UITextField *)textfield{
    
    if (self.myBlock != NULL){
        
        if (_numOfWord) {
            
            if (textfield.text.length>_numOfWord) {
                
            
             self.myBlock([textfield.text substringToIndex:_numOfWord]);
            }else{
                  self.myBlock(textfield.text);
            }
            
        }else{
        
        self.myBlock(textfield.text);
        }
    
    }
    
    if (textfield.text.length>=1) {
        self.selected = YES;
    }else{
        
        self.selected = NO;
        
    }
    
    
}


@end
